package main

import (
	"fmt"
)

func goStructure() {
	testStructure()
	// testString()
	// testArray()
	// testMap()
	// testPointer()

	//testStruct()
	//testMethod()
}

func testStructure() {

	var i = 10
	fmt.Println(i)

	s := "string"
	fmt.Println(s)

}

func testString() {
	// 字符串：不可变的，无法修改
	str := "hello world\n\tgolang native string"
	fmt.Println(str)

	nativeString := `hello world
	golang native string`
	fmt.Println(nativeString)

	// bytes := []byte(str)
	// fmt.Println(bytes)

}

func testArray() {
	strings := []string{"A", "B", "C"}

	for i, s := range strings {
		fmt.Println(i, s)
	}

	var a []string
	var p = new([]string) // *[]string
	a = *p
	p = &a

	v := make([]int, 3, 5)
	fmt.Println(v, len(v), cap(v))
	v = v[:cap(v)] // 切片重组reslicing
	fmt.Println(v, len(v), cap(v))
	x := make([]int, 3)
	l := copy(v, x)
	fmt.Println(l)
	v = append(v, 1, 2, 3)
	fmt.Println(v, len(v), cap(v))

}

func testMap() {
	// 并发不安全的
	m := map[string]string{
		"key": "value",
	}
	m["a"] = "A"

	for key, value := range m {
		fmt.Println(key, value)
	}

	key := "key"
	if value, exist := m[key]; exist {
		fmt.Println(value)
	}

	delete(m, key)

}

func testPointer() {

	i := 10
	p := &i
	fmt.Println(*p)

	str := "str"
	var ptr *string
	// 分配内存
	ptr = new(string)
	*ptr = str
	fmt.Println(*ptr)

}
